<template>
  <div>
    <el-card class="box-card">

      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="方向名称">
          <el-input v-model="formInline.name" placeholder="方向名称"></el-input>
        </el-form-item>



        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="toAdd">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="方向姓名"
          width="180">
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template v-slot:="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" @click="deleteCollege(scope.row.id)" size="small">删除</el-button>
          </template>
        </el-table-column>


      </el-table>
    </el-card>



    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.page"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>


    <!-- Form -->

    <el-dialog title="修改方向" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="方向名称" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off"></el-input>
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCollege">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
  tableData: [],
      formInline: {
        name: '',
        page: 1,
        size:2
      },
      total:10,//展示多少条数据
      dialogFormVisible: false,
      formLabelWidth: '120px',
 form:{
id:1,
   name:''
     }
    }

  },
  created() {
   this.showList()
  },
  methods: {

    deleteCollege(id){
      this.$axios.get('college/delete?id='+id).then((res)=>{
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
        this.showList()
      })
    },

//
    updateCollege(){
this.$axios.post('college/update',this.form).then((res)=>{
  console.log(res)
  this.$message({
    message: '恭喜你，修改成功',
    type: 'success'
  });
  this.dialogFormVisible = false
    this.showList()
})
    },




    //点击事件
    handleClick(row) {
      this.dialogFormVisible = true
      this.form.id = row.id
      this.form.name = row.name
    },

    toAdd(){
      this.$router.push("CollegeAdd")
    },



    showList() {

      this.$axios.post("college/searchPage", this.formInline).then((res) => {
        console.log(res.data.data)
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      })
    },
    onSubmit() {
      this.formInline.page=1
     this.showList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);

      this.formInline.size=val
      this.showList()

    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.page=val
      this.showList()

    }
  }

}


</script>

<style>

</style>
